using System;
using System.Windows.Input;

namespace FlynnGui.Infrastructure
{
	public class DelegateCommand : ICommand
	{
		public delegate void SimpleEventHandler(object sender, DelegateCommandEventArgs e);

		private readonly SimpleEventHandler _handler;
		private bool _isEnabled = true;

		public DelegateCommand(SimpleEventHandler handler)
		{
			_handler = handler;
		}

		#region ICommand implementation

		public void Execute(object parameter)
		{
			_handler(this, new DelegateCommandEventArgs(parameter));
		}

		public bool CanExecute(object parameter)
		{
			return IsEnabled;
		}

		public event EventHandler CanExecuteChanged;

		#endregion

		public bool IsEnabled
		{
			get { return _isEnabled; }
			set
			{
				_isEnabled = value;
				OnCanExecuteChanged();
			}
		}

		private void OnCanExecuteChanged()
		{
			if (CanExecuteChanged != null)
			{
				CanExecuteChanged(this, EventArgs.Empty);
			}
		}
	}
}